package com.example.demo.web;

import com.example.demo.handler.UserExcelHandler;
import com.example.demo.module.UserExcel;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.result.ExcelImportResult;
import org.jeecgframework.poi.handler.inter.IExcelDataHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author:lgl
 * @Description:
 * @Date:Create in 2018/1/24 .
 */
@RestController
public class UserExcelController {

    private static final Logger log = LoggerFactory.getLogger(UserExcelController.class);

    @PostMapping("excelImport.do")
    public void excelImport(@RequestParam("file") MultipartFile file) {
        ImportParams importParams = new ImportParams();
        // 数据处理
        IExcelDataHandler<UserExcel> handler = new UserExcelHandler();
        handler.setNeedHandlerFields(new String[] { "姓名" });// 注意这里对应的是excel的列名。也就是对象上指定的列名。
        importParams.setDataHanlder(handler);

        // 需要验证
        //importParams.setNeedVerfiy(true);

        try {
            ExcelImportResult<UserExcel> result = ExcelImportUtil.importExcelVerify(file.getInputStream(), UserExcel.class,
                    importParams);
            System.out.println(result.getList());
            List<UserExcel> successList = result.getList();

            //List<UserExcel> failList = result.getFailList();

            log.info("是否存在验证未通过的数据:" + result.isVerfiyFail());
            log.info("验证通过的数量:" + successList.size());
            //log.info("验证未通过的数量:" + failList.size());

            for (UserExcel user : successList) {
                log.info("成功列表信息:ID=" + user.getId() + user.getName() + "-"
                        + new SimpleDateFormat("yyyy-MM-dd").format(user.getBirthday()));
            }
//            for (User user : failList) {
//                log.info("失败列表信息:" + user.getName());
//            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @GetMapping("export.do")
    public void export(HttpServletResponse response) {
        try {
            // 设置响应输出的头类型
            response.setHeader("content-Type", "application/vnd.ms-excel");
            // 下载文件的默认名称
            response.setHeader("Content-Disposition", "attachment;filename=user.xls");
            // =========easypoi部分
            ExportParams exportParams = new ExportParams();
            // exportParams.setDataHanlder(null);//和导入一样可以设置一个handler来处理特殊数据
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, UserExcel.class, data());
            workbook.setSheetName(0,"aaaaaaaaaaaaa");
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /***
     * 创建模拟数据
     *
     * @return
     */
    private List<UserExcel> data() {
        List<UserExcel> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add(new UserExcel("id-" + i, "李四-" + i, 15 + i, new Date()));
        }
        return list;
    }
}
